
static int bll_error_handler(lua_State *L) {
	lua_getfield(L, LUA_GLOBALSINDEX, "_bllua_on_error");
	if (!lua_isfunction(L, -1)) {
		BlPrintf("no onError");
		lua_pop(L, 1);
		return 1;
	}
	
	int hpos = lua_gettop(L) - 1;
	lua_insert(L, hpos); // move function to before arg
	
	lua_pcall(L, 1, 1, 0);
	return 1;
}

int bll_pcall( lua_State* L, int nargs, int nret ) {
	/* calculate stack position for message handler */
	int hpos = lua_gettop( L ) - nargs;
	int ret = 0;
	/* push custom error message handler */
	lua_pushcfunction( L, bll_error_handler );
	/* move it before function and arguments */
	lua_insert( L, hpos );
	/* call lua_pcall function with custom handler */
	ret = lua_pcall( L, nargs, nret, hpos );
	/* remove custom error message handler from stack */
	lua_remove( L, hpos );
	/* pass return value of lua_pcall */
	return ret;
	
	lua_pcall(L, nargs, nret, 0);
}
